/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Config.ConnectionConfig;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import product.generate.ProductList;
import product.generate.Products;
import user.generate.UserList;
import user.generate.Users;

/**
 *
 * @author NganNLK60334
 */
public class AdminDAO {

    public static Products getAllProducts() throws ClassNotFoundException, SQLException {
        String sql = "Select * from Products";
        Connection con = null;
        ResultSet rs = null;
        Products products = new Products();
      
        try {
            con = ConnectionConfig.getConnection();
            PreparedStatement stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            while (rs.next()) {
                ProductList product = new ProductList();
                product.setProductID(new BigInteger(Integer.valueOf(rs.getInt("productID")).toString()));
                product.setProductName(rs.getString("productName"));
                product.setCategoryID(new BigInteger(Integer.valueOf(rs.getInt("categoryID")).toString()));
                product.setPrice(new BigInteger(Long.valueOf(rs.getInt("price")).toString()));
                product.setProductImg(rs.getString("productImg"));
                product.setDescription(rs.getString("description"));
                product.setIsShow(rs.getBoolean("isShow"));
                product.setQuantityInStock(new BigInteger(Integer.valueOf(rs.getInt("quantityInStock")).toString()));
                products.getProduct().add(product);

            }
            return products;
        } catch (Exception ex) {
            Logger.getLogger(AdminDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return null;
    }
    public static Users getAllUsers() throws ClassNotFoundException, SQLException {
        String sql = "Select * from Users";
        Connection con = null;
        ResultSet rs = null;
        Users users = new Users();

        try {
            con = ConnectionConfig.getConnection();
            PreparedStatement stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            while (rs.next()) {
                UserList user = new UserList();
                user.setUsername(rs.getString("username"));
                user.setFullname(rs.getString("fullname"));
                user.setGender(rs.getBoolean("gender"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setIsActive(rs.getBoolean("isActive"));
                users.getUser().add(user);

            }
            return users;

        } catch (Exception ex) {
            Logger.getLogger(AdminDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return null;
    }
public static boolean updateStatusProduct(ProductList protype) throws SQLException {
        Connection con = null;
        try {
            String sql = "update Products set isShow =? where productID =?";
            con = ConnectionConfig.getConnection();
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setBoolean(1, Boolean.valueOf(protype.isIsShow()));
            stm.setInt(2, Integer.parseInt(protype.getProductID().toString()));
            return stm.executeUpdate() > 0;
        } catch (Exception e) {
            Logger.getLogger(AdminDAO.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            if (con != null) {
                con.close();
            }
        }
        return false;

    }
    public static boolean updateStatusUser(UserList usertype) throws SQLException {
        Connection con = null;
        try {
            String sql = "update Users set isActive=? where username =?";
            con = ConnectionConfig.getConnection();
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setBoolean(1, Boolean.valueOf(usertype.isIsActive()));
            stm.setString(2, usertype.getUsername());
            return stm.executeUpdate() > 0;
        } catch (Exception e) {
            Logger.getLogger(AdminDAO.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            if (con != null) {
                con.close();
            }
        }
        return false;

    }
     public boolean updateProducts(int productID, String productName, String price, String description, int quantityInStock) {
        Connection con = null;
         try {
            java.sql.Connection conn = null;
            conn = ConnectionConfig.getConnection();

            String sql = "Update Products set productName = ?, price = ?, description=?, quantityInStock=? where productID=?";

            PreparedStatement stm = conn.prepareStatement(sql);
            stm.setString(1, productName);
            stm.setString(2, price);
            stm.setString(3, description);
            stm.setInt(4, quantityInStock);
            stm.setInt(5, productID);
            int result = stm.executeUpdate();
            stm.close();
            conn.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return false;
    }
     public int insertProducts(String productName, int categoryID, String price, String productImg,
            String description, boolean isShow, int quantityInStock) {
        int productId = -1;
        try {
             java.sql.Connection conn = null;
            conn = ConnectionConfig.getConnection();
            String sql = "Insert Into Products(productName, categoryID, price, productImg, description, isShow, quantityInStock)"
                    + " Values (?,?,?,?,?,?,?)";
            PreparedStatement stm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            stm.setString(1, productName);
            stm.setInt(2, categoryID);
            stm.setString(3, price);
            stm.setString(4, productImg);
            stm.setString(5, description);
            stm.setBoolean(6, isShow);
            stm.setInt(7, quantityInStock);
            stm.executeUpdate();
            ResultSet resultID = stm.getGeneratedKeys();
            while (resultID.next()) {
                productId = resultID.getInt(1);
            }
            stm.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return productId;
    }


}
